/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.ical.repository;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.ical.IEventStore;
import cn.ekuma.epos.ical.event.OrderEvent;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBF;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.data.loader.query.QBFParameter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class OrderEventStore implements IEventStore{
    Object[] filter;
    I_DataLogicERP m_dlDales;

    public OrderEventStore(I_DataLogicERP m_dlDales,Object[] filter) {
        this.filter = filter;
        this.m_dlDales = m_dlDales;
    }

    String[] fields=new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE"};

    @Override
    public List getEventInstancesForDate(Date start, Date end) {
        filter[6] =  QBFCompareEnum.COMP_GREATEROREQUALS;
        filter[7] = start;
        filter[8] = QBFCompareEnum.COMP_LESS;
        filter[9] = end;
        List<FindOrdersInfo> findOrderInfo=null;
        List<OrderEvent> eventList=new ArrayList();

        QBF qbf=QBF.c();
        for(int i=0;i<filter.length;i=i+2)
            if(filter[i]!=QBFCompareEnum.COMP_NONE)
                qbf.and(new QBFParameter(fields[i/2],(QBFCompareEnum) filter[i],filter[i+1]));
        try {
            findOrderInfo=m_dlDales.query(FindOrdersInfo.class,qbf.build());
        } catch (BasicException ex) {
            Logger.getLogger(OrderEventStore.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(findOrderInfo!=null){
            for(FindOrdersInfo order:findOrderInfo)
                eventList.add(new OrderEvent(order));
        }
        return eventList;
    }
}
